package com.neusoft.elm.controller;

import com.neusoft.elm.po.Cart;
import com.neusoft.elm.service.CartService;
import com.neusoft.elm.service.impl.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class CartController {
    CartService cartService=new CartServiceImpl();

//    按照条件查询购物车记录列表
    public Object listCart(HttpServletRequest request)throws Exception{
        Integer businessId = null;
    String userId=request.getParameter("userId");
    String businessIdStr=request.getParameter("businessId");
    if (businessIdStr!=null){
        businessId=Integer.valueOf(businessIdStr);
    }
    Cart cart=new Cart();
    cart.setBusinessId(businessId);
    cart.setUserId(userId);
        List<Cart> list = cartService.listCart(cart);
        return list;

    }
//    添加一条购物车记录
    public Object saveCart(HttpServletRequest request)throws Exception{

        Integer foodId = Integer.valueOf(request.getParameter("foodId"));
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));

        String userId = request.getParameter("userId");
        Cart cart=new Cart();
        cart.setUserId(userId);
        cart.setBusinessId(businessId);

        cart.setFoodId(foodId);

        int n=cartService.saveCart(cart);
        return n;

    }
//    按照条件删除一条购物车记录
    public Object removeCart(HttpServletRequest request)throws Exception{
        Integer foodId = Integer.valueOf(request.getParameter("foodId"));
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));

        String userId = request.getParameter("userId");
        Cart cart=new Cart();
        cart.setUserId(userId);
        cart.setBusinessId(businessId);

        cart.setFoodId(foodId);
        int n=cartService.removeCart(cart);
        return n;
    }
//    按照条件修改购物车记录数
    public Object updateCart(HttpServletRequest request)throws Exception{
        Integer foodId = Integer.valueOf(request.getParameter("foodId"));
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        Integer quantity =Integer.valueOf( request.getParameter("quantity"));
        String userId = request.getParameter("userId");
        Cart cart=new Cart();
        cart.setUserId(userId);
        cart.setBusinessId(businessId);
        cart.setQuantity(quantity);
        cart.setFoodId(foodId);
        int n = cartService.removeCart(cart);
        return n;


    }
}
